﻿namespace Harness.Commands
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using mshtml;
    using SHDocVw;

    internal class SwitchFrameCommand : ICommand
    {
        #region Constants

        private const int NameArgIndex = 0;

        #endregion

        #region Public Methods

        public void SyntaxCheck(IHarness harness, IEnumerable<object> args)
        {
            if (null == args.ElementAtOrDefault(NameArgIndex))
            {
                throw new ArgumentNullException((NameArgIndex + 1).ToString() + ":Frame Name");
            }
        }

        public dynamic Execute(IHarness harness, IEnumerable<object> args)
        {
            string name = (args.ElementAtOrDefault(NameArgIndex) ?? string.Empty).ToString().ToLower();

            IE.WaitForReadyStateComplete();

            switch (name)
            {
                case "_parent":
                    IE.Frame.SwitchToParent();
                    break;

                case "_top":
                    IE.Frame.SwitchToTop();
                    break;

                default:
                    IE.Frame.SwtichTo(name);
                    break;
            }

            return null;
        }

        #endregion
    }
}
